﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Game.Types;

namespace Game.Editor.Toolbox
{
    public partial class VehicleItemControl2 : UserControl
    {
        readonly Image _Image = new Image();
        readonly Button _Button = new Button();

        MapItemType _MapItemType;

        public VehicleItemControl2()
        {
            InitializeComponent();

            SelectedVehicleType.Selected += delegate
            {
                UpdatePressedState();
            };
        }

        void InitializeComponent()
        {
            Canvas canvas = new Canvas();
            this.Content = canvas;

            _Button.Width = 50;
            _Button.Height = 50;
            Canvas.SetLeft(_Button, 0);
            Canvas.SetTop(_Button, 0);
            _Button.Click += Button_Click;
            canvas.Children.Add(_Button);

            _Button.Content = _Image;
        }

        public Brush ToolColor
        {
            get { return _Button.Background; }
            set { _Button.Background = value; }
        }

        public MapItemType MapItemType
        {
            get { return _MapItemType; }
            set { _MapItemType = value; DisplayType(); }
        }

        void DisplayType()
        {
            if (_MapItemType != null)
            {
                if (_MapItemType.Picture != null)
                {
                    _Image.Source = _MapItemType.Picture;

                    ToolTip t = new ToolTip();
                    t.Content = "" + _MapItemType.Name;
                    ToolTipService.SetToolTip(_Button, t);
                }
            }
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            SelectedVehicleType.VehicleType = _MapItemType as VehicleType;
        }

        void UpdatePressedState()
        {
            if (SelectedVehicleType.VehicleType == this._MapItemType)
            {
                _Button.Background = new SolidColorBrush(SystemColors.HighlightColor);
            }
            else
            {
                _Button.Background = new SolidColorBrush(SystemColors.ControlLightColor);
            }
        }

    }
}
